import { defineComponent, ref, reactive } from "vue";
import TablePage from "@/components/TablePage/TablePage";
import { tablesInterface } from "@/common/interface/table.interface";
import MusicSongCategoryService from "./index.service";
import Add from "./Add/Add";
import StatusTag from "@/components/StatusTag/StatusTag";
export default defineComponent({
  name: "Category",
  setup() {
    const tablePageRef = ref(null as any);
    // 表格定义
    const tables = reactive({
      list: [
        {
          title: "曲谱分类",
          rowKey: "id",
          columns: [
            {
              title: "ID",
              dataIndex: "id",
              align: "center",
              width: "80px",
            },
            {
              title: "分类名",
              dataIndex: "name",
              align: "center",
              width: "220px",
            },
            {
              title: "排序",
              dataIndex: "sort",
              align: "center",
              width: "100px",
            },
            {
              title: "状态",
              dataIndex: "status",
              align: "center",
              width: "180px",
            },
          ],
          toolbar: {
            left: ["refresh", "add"],
            right: ["filter"],
            add: {
              permissions: "music:songCategory:add",
              handler: () => {
                const addPopupRef = tablePageRef.value.getPopup("addRef");
                addPopupRef.value.open({ type: 'add' });
              }
            },
          },
          service: new MusicSongCategoryService(),
          operationWidth: "200px",
          operation: ({record}) => [
            {
              type: "add",
              text: "添加子分类",
              permissions: "music:songCategory:add",
              clickHandler: () => {
                const addPopupRef = tablePageRef.value.getPopup("addRef");
                addPopupRef.value.open({ type: 'add', parentId: record.id });
              }
            },
            {
              type: "edit",
              permissions: "music:songCategory:edit",
              clickHandler: () => {
                const addPopupRef = tablePageRef.value.getPopup("addRef");
                addPopupRef.value.open({ type: 'edit', id: record.id });
              }
            },
            {
              type: "delete",
              permissions: "music:songCategory:remove",
              clickHandler: "delete",
            },
          ],
          bodyCell: ({ text, record, index, column }) => {
            if (column.dataIndex === "status") {
              return <StatusTag text={text.toString()} />
            }
          },
          popups: [
            {
              ref: "addRef",
              dom: () => <Add onChange={() => { tablePageRef.value.reload() }} />,
            }
          ],
          pagination: true,
          tableSourceFormat: {
            data: 'rows',
            pageSize: 'pageSize',
            current: 'pageNum',
          },
        }
      ],
    } as tablesInterface)
    // 配置项
    const config = reactive({
      table: tables.list[0],
    })
    return () => (
      <TablePage ref={tablePageRef} config={config} />
    )
  }
})